﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataTypeInfoDao : DaoService<MetaDataTypeInfo>
    {
        public MetaDataTypeInfoDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_TypeInfo";
            }
        }

        //public override Dictionary<string, object> GetDataMapping(MetaDataTypeInfo data)
        //{
        //    var datas = base.GetDataMapping(data);
        //    datas.Remove("MetaDataType");
        //    datas.Add("MetaDataType", data.MetaDataType.ToString());

        //    return datas;
        //}

        //public override MetaDataTypeInfo ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        //{
        //    var myCol = new Dictionary<string, PropertyInfo>(base.Columns);

        //    myCol.Remove("MetaDataType");

        //    var data = base.ConvertToObject(myCol, row);

        //    if (row["MetaDataType"] != DBNull.Value)
        //        data.MetaDataType = (MetaDataType)Enum.Parse(typeof(MetaDataType),
        //                                        Convert.ToString(row["MetaDataType"]));
        //    else
        //        data.MetaDataType = MetaDataType.Other;

        //    return data;
        //}

        public MetaDataTypeInfo GetMetaDataTypeInfo(string metaTypeID)
        {
            var sql = string.Format("select * from {0} where ID='{1}' and IsDeleted=0", TableName, metaTypeID);
            var ds = DB.ExecuteDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                var row = ds.Tables[0].Rows[0];

                return ConvertToObject(this.Columns, row);
            }

            return null;
        }

        public List<MetaDataTypeInfo> GetMetaDataTypeInfos()
        {
            var result = new List<MetaDataTypeInfo>();
            List<object> objects = base.GetObjectsByFilter($"IsDeleted=0");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as MetaDataTypeInfo));
            }
            return result;
        }

        /// <summary>
        /// 获取元数据类型对应的序号
        /// </summary>
        /// <param name="metaTypeID"></param>
        /// <returns></returns>
        public int GetMetaDataTypeSortOrder(string metaTypeID)
        {
            var sql = string.Format("select SortCode from {0} where ID='{1}' and IsDeleted=0", TableName, metaTypeID);
            var ds = DB.ExecuteDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                var row = ds.Tables[0].Rows[0];
                return Convert.ToInt32(row["SortCode"].ToString());
            }

            return 0;
        }
    }
}
